<?php

use Biz\service\brand\BrandGrowth;
use Biz\service\brand\BrandHandleTask;
use Doraemon\model\Photos;
use Doraemon\tools\Log;

class LevelAction extends ActionPlugin
{
    const IS_TRACING = true;

    /**
     * @throws Exception
     */
    public function main()
    {
        $message = $this->messageBody;
        $table = $this->messageBody['table'] ?? '';
        $uid = 0;
        $extend = [];
        $event = '';
        if (strstr($table, Photos::_TABLE_NAME)) {
            $uid = $this->messageBody['data']['uid'] ?? 0;
            $type = $this->messageBody['type'] ?? '';
            $quality = $this->messageBody['data']['quality'] ?? Photos::QUALITY_PENDING;
            $admin_score = $this->messageBody['data']['admin_score'] ?? 0;
            if ($type == 'insert') {
                BrandGrowth::dailyTaskPublish($uid);
                BrandHandleTask::getInstance($uid)->selectTask(BrandHandleTask::TASK_TYPE_PUBLISH)->finish();
                $event = '发布';
            } elseif ($type == 'update') {
                $featuredQuality = [Photos::QUALITY_FEATURED, Photos::QUALITY_HIGH];
                if (isset($this->messageBody['old']['quality'])
                    && !in_array($this->messageBody['old']['quality'], $featuredQuality)
                    && in_array($quality, $featuredQuality)) {
                    BrandGrowth::levelTaskPublish($uid);
                    BrandHandleTask::getInstance($uid)->selectTask(BrandHandleTask::TASK_TYPE_PUBLISH)->finish();
                    $event = '发布精选';
                }
            }
            $extend = compact('quality', 'admin_score', 'type');
        } elseif (strstr($table, \Doraemon\model\Comment::_TABLE_NAME)) {
            $uid = $this->messageBody['data']['uid'] ?? 0;
            BrandGrowth::dailyTaskReviewed($uid);
            BrandHandleTask::getInstance($uid)->selectTask(BrandHandleTask::TASK_TYPE_REVIEWED)->finish();
            $event = '评论';
        } elseif (strstr($table, \Doraemon\model\Like::_TABLE_NAME)) {
            $uid = $this->messageBody['data']['uid'] ?? 0;
            $ouid = $this->messageBody['data']['ouid'] ?? 0;
            if ($uid != $ouid) {
                BrandGrowth::dailyTaskLiked($uid);
                BrandHandleTask::getInstance($uid)->selectTask(BrandHandleTask::TASK_TYPE_LIKED)->finish();
                $event = '点赞';
            }
        } elseif (strstr($table, \Biz\model\brand\Content::_TABLE_NAME)) {
            $brand_id = $this->messageBody['data']['brand_id'] ?? 0;
            $wiki_num = $this->messageBody['data']['wiki_num'] ?? 0;
            $claim_type = $this->messageBody['old']['claim_type'] ?? -1;
            $new_claim_type = $this->messageBody['data']['claim_type'] ?? 0;
            $brand_uid = $this->messageBody['data']['brand_uid'] ?? 0;
            $is_selected = $this->messageBody['data']['is_selected'] ?? 0;
            $old_is_selected = $this->messageBody['old']['is_selected'] ?? -1;
            $extend = compact('brand_id', 'claim_type', 'wiki_num', 'brand_uid', 'is_selected', 'old_is_selected');
            if ($old_is_selected == 0 && $is_selected == 1) {
                BrandGrowth::dailyTaskFeatured($uid, $brand_id);
                BrandHandleTask::getInstance(0, $brand_id)->selectTask(BrandHandleTask::TASK_TYPE_FEATURED)->finish();
                $event = '内容精选';
            } else {
                //无需认领
                if ($claim_type == 0 && $new_claim_type == \Biz\model\brand\Content::CLAIM_TYPE_CONTENT_ONLY) {
                    BrandGrowth::dailyTaskClaim($uid, $brand_id);
                    BrandHandleTask::getInstance(0, $brand_id)->selectTask(BrandHandleTask::TASK_TYPE_CLAIM)->finish();
                    $event = '认领内容';
                } elseif ($claim_type == 0 && $new_claim_type == \Biz\model\brand\Content::CLAIM_TYPE_BOTH) {
                    BrandGrowth::dailyTaskClaim($uid, $brand_id);
                    BrandGrowth::dailyTaskClaimWiki($uid, $brand_id);
                    BrandHandleTask::getInstance(0, $brand_id)->selectTask(BrandHandleTask::TASK_TYPE_CLAIM)->finish();
                    BrandHandleTask::getInstance(0, $brand_id)->selectTask(BrandHandleTask::TASK_TYPE_CLAIM_WIKI)->finish();
                    $event = '认领内容绑定wiki';
                } elseif ($claim_type == 0 && $new_claim_type == \Biz\model\brand\Content::CLAIM_TYPE_APPEND_ONLY) {
                    if ($brand_uid == 0) {
                        BrandGrowth::dailyTaskClaimWiki($uid, $brand_id);
                        BrandHandleTask::getInstance(0, $brand_id)->selectTask(BrandHandleTask::TASK_TYPE_CLAIM_WIKI)->finish();
                        $event = '绑定wiki';
                    }
                } elseif ($new_claim_type == \Biz\model\brand\Content::CLAIM_TYPE_NONE) {
                    if ($brand_uid == 0) {
                        if ($claim_type == \Biz\model\brand\Content::CLAIM_TYPE_CONTENT_ONLY) {
                            if ($wiki_num > 0) {
                                BrandGrowth::levelTaskClaim($uid, $brand_id);
                                $event = '认领带wiki内容';
                            }
                        } elseif ($claim_type == \Biz\model\brand\Content::CLAIM_TYPE_BOTH) {
                            BrandGrowth::levelTaskClaim($uid, $brand_id);
                            $event = '认领带wiki内容';
                        } elseif ($claim_type == \Biz\model\brand\Content::CLAIM_TYPE_APPEND_ONLY) {
                            BrandGrowth::levelTaskClaim($uid, $brand_id);
                            $event = '认领带wiki内容';
                        }
                    }
                }
            }
        }

        Log::info("接收到事件消息#uid:[$event]" . $uid, compact('uid', 'message') + $extend);
        $this->setSuccess();
    }
}